import Vue from 'vue'
import VueRouter from 'vue-router'
import UserHome from '@/components//home/UserHome.vue'
import UserMall from '@/components/mall/UserMall.vue'
import ShopCart from '@/components//shopcart//ShopCart.vue'
import UserInfo from '@/components//user/UserInfo.vue'
import UserLogin from '@/components/login/UserLogin.vue'
import UserForget from '@/components/login/UserForget.vue'
import EditUserInfoVue from '@/components/user/userinfo/EditUserInfo.vue'
import AllAddress from '@/components//user/address/AllAddress.vue'
import AddAddress from '@/components/user/address/AddAddress.vue'
import EditAddress from '@/components/user/address/EditAddress.vue'
import SecureCenter from '@/components/user/secure/SecureCenter.vue'
import GoodsDetail from '@/components/detail/GoodsDetail.vue'
import SearchPage from '@/components/search/SearchPage.vue'
import MoreNewGoods from '@/components/home/MoreNewGoods.vue'
import MoreRecommendGoods from '@/components/home/MoreRecommendGoods.vue'
import UserCollection from '@/components/user/like/UserCollection.vue'
import OrderPayment from '@/components/user/order/OrderPayment.vue'
import AllOrderList from '@/components/user/order/AllOrderList.vue'
import OrderListPage from '@/components/user/order/OrderListPage.vue'

Vue.use(VueRouter)

// 重写push方法
const originalPush = VueRouter.prototype.push
VueRouter.prototype.push = function push(location, resolve, reject) {
  if (resolve || reject) return originalPush.call(this, location, resolve, reject)
  return originalPush.call(this, location).catch(() => {})
}

// 创建路由数组
const routes = [
  {
    path: '',
    redirect: '/home'
  },
  {
    path: '/',
    redirect: '/home'
  },
  // 首页路由规则
  {
    path: '/home',
    component: UserHome
  },
  // 商城路由规则
  {
    path: '/mall',
    component: UserMall
  },
  // 购物车路由规则
  {
    path: '/shopcart',
    component: ShopCart
  },
  // 个人中心路由规则
  {
    path: '/userinfo',
    component: UserInfo
  },
  // 登录页面路由规则
  {
    path: '/login',
    component: UserLogin
  },
  // 忘记密码页面路由规则
  {
    path: '/forget',
    component: UserForget
  },
  // 编辑用户资料
  {
    path: '/account',
    component: EditUserInfoVue
  },
  // 地址管理
  {
    path: '/address',
    component: AllAddress
  },
  // 新增收货地址
  {
    path: '/addaddress',
    component: AddAddress
  },
  // 修改收收货地址
  {
    path: '/editaddress/:id',
    component: EditAddress
  },
  // 安全中心
  {
    path: '/secure',
    component: SecureCenter
  },
  // 商品详情
  {
    path: '/goods/detail/:id',
    component: GoodsDetail,
    props: true
  },
  // 搜索页面
  {
    path: '/search',
    component: SearchPage
  },
  // 推荐商品
  {
    path: '/recommend',
    component: MoreRecommendGoods
  },
  // 最新商家商品
  { path: '/new', component: MoreNewGoods },
  // 我的收藏
  {
    path: '/like',
    component: UserCollection
  },
  // 订单支付页面
  {
    path: '/order/payment/:id',
    component: OrderPayment
  },
  // 我的订单页面
  {
    path: '/order',
    component: AllOrderList
  },
  // 所有订单验证规则
  {
    path: '/all/order',
    component: OrderListPage
  }
]

// 创建路由对象
const router = new VueRouter({
  routes
})

// 设置路由前置导航守卫
router.beforeEach((to, from, next) => {
  const paths = ['/login', '/home', '/forget', '/recommend', '/new', '/mall', '/search']
  console.log(/^\/goods\/detail\//.test(to.path))
  if (paths.indexOf(to.path) === -1 && !/^\/goods\/detail\//.test(to.path)) {
    if (!localStorage.getItem('user_token')) {
      next('/login')
    } else {
      next()
    }
  } else {
    next()
  }

  next()
})

export default router
